package com.itlong.cloud.app.service.impl;

import com.itlong.cloud.POJO.DTO.app.engineer.AppEngineerDevicesDTO;
import com.itlong.cloud.POJO.DTO.app.engineer.AppEngineerThresholdDTO;
import com.itlong.cloud.POJO.VO.app.engineer.AppEngineerDevicesVO;
import com.itlong.cloud.POJO.VO.app.engineer.AppEngineerDoorDevicesVO;
import com.itlong.cloud.POJO.VO.app.engineer.AppEngineerFloorDevicesVO;
import com.itlong.cloud.POJO.VO.app.engineer.AppEngineerQrCodeDeviceVO;
import com.itlong.cloud.app.dao.IAppEngineerDeviceDao;
import com.itlong.cloud.app.service.IAppEngineerDeviceService;
import com.itlong.cloud.enumerate.PropertyEquipTypeEnum;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      工程版App设备信息业务处理接口实现类。
 * <desc/>
 *
 * @createDate 2017/11/8.
 */
@Service
public class AppEngineerDeviceServiceImpl implements IAppEngineerDeviceService{

    @Autowired
    IAppEngineerDeviceDao iAppEngineerDeviceDao;

    @Autowired
    RedisService redisService;

    /**
     * <desc>
     *      获取用户门禁设备列表
     * </desc>
     *
     * @param userId 用户Id
     * @param projectId 项目Id
     * @return
     * @author shengen.T
     * @createDate 2017/11/8
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<AppEngineerDoorDevicesVO> getDoorDevices(String userId, String projectId) throws Exception{
        List<AppEngineerDoorDevicesVO> list= iAppEngineerDeviceDao.getDoorDevices(projectId);
        return list;
    }

    /**
     * <desc>
     *      获取用户电梯设备列表
     * </desc>
     *
     * @param userId 用户Id
     * @param projectId 项目Id
     * @return
     * @author shengen.T
     * @createDate 2017/11/8
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<AppEngineerFloorDevicesVO> getFloorDevices(String userId,String projectId) throws Exception{
        List<AppEngineerFloorDevicesVO> list= iAppEngineerDeviceDao.getFloorDevices(projectId);
        for(AppEngineerFloorDevicesVO vo : list){
            if(StringUtils.isNotBlank(vo.getFloorNum())){
                Integer floor = Integer.valueOf(vo.getFloorNum());
                if(floor>0) {
                    vo.setFloorNum(StringHandlerUtil.autlSupply(2,Integer.toHexString(floor),"0"));
                }else{
                    vo.setFloorNum("F"+Math.abs(floor));
                }
            }
        }
        return list;
    }


     /**
      * <desc>
      *      获取设备机号列表信息。
      * <desc/>
      *
      * @param  devicesDTO 设备机号请求参数
      * @return
      * @author shengen.T
      * @createDate 2017/11/8
      */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<AppEngineerDevicesVO> getDeviceId(AppEngineerDevicesDTO devicesDTO) throws Exception{
        Integer diveceType = Integer.parseInt(devicesDTO.getDeviceType());
        List<AppEngineerDevicesVO> list=null;
            //判断设备类型 1电梯 2门禁
            if(diveceType== PropertyEquipTypeEnum.ELEVATOR.getType()){
                list = iAppEngineerDeviceDao.getFloorDeviceId(devicesDTO.getDeviceName(),devicesDTO.getProjectId());
            }else if(diveceType == PropertyEquipTypeEnum.ENTRANCE.getType()){
                list = iAppEngineerDeviceDao.getDoorDeviceId(devicesDTO.getDeviceName(),devicesDTO.getProjectId());
            }else if(diveceType == PropertyEquipTypeEnum.OUT_CALL_CONTROLLER.getType()){
                list = iAppEngineerDeviceDao.getOutCallDeviceId(devicesDTO.getDeviceName(),devicesDTO.getProjectId());
            }
        return list;
    }

     /**
      * <desc>
      *      上传阈值。
      * <desc/>
      *
      * @param  thresholdDTO 上传阈值请求参数
      * @return
      * @author shengen.T
      * @createDate 2017/11/9
      */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer uploadData(AppEngineerThresholdDTO thresholdDTO) throws Exception{
        Integer diveceType = Integer.parseInt(thresholdDTO.getDeviceType());
        Integer isSuccess =0;//成功标志 0失败 1成功
        //判断设备类型 1电梯 2门禁
        if(diveceType == PropertyEquipTypeEnum.ELEVATOR.getType()){
             isSuccess = iAppEngineerDeviceDao.uploadElevatorData(thresholdDTO.getDeviceUnique(),thresholdDTO.getSignal());
        }else if(diveceType == PropertyEquipTypeEnum.ENTRANCE.getType()){
            isSuccess = iAppEngineerDeviceDao.uploadEntranceData(thresholdDTO.getDeviceUnique(),thresholdDTO.getSignal());
        }
        return isSuccess;
    }

    /**
     * <desc>
     *      获取二维码绑定的设备信息
     * </desc>
     *
     * @return 二维码绑定的设备信息
     * @author wangzhi
     * @createDate 2020/02/26
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public AppEngineerQrCodeDeviceVO getDeviceByQrCode(String qrCodeContent) throws Exception {
        return iAppEngineerDeviceDao.getDeviceByQrCode(qrCodeContent);
    }

    /**
     * <desc>
     *      绑定设备与二维码
     * </desc>
     *
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2020/02/26
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer bindingDeviceQrCode(String qrCodeContent, String projectId, String deviceType, String sedanPlace, String deviceUnique, String floor, String userId) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("qrCodeContent", qrCodeContent);
        params.put("projectId", projectId);
        params.put("deviceType", deviceType);
        params.put("sedanPlace", StringUtils.isBlank(sedanPlace) ? null : sedanPlace);
        params.put("deviceUnique", deviceUnique);
        params.put("userId", userId);
        Integer count = iAppEngineerDeviceDao.getDeviceCountByParams(params);
        if (count > 0) {
            if ("1".equals(deviceType) && "2".equals(sedanPlace)) {
                String naturalFloor = iAppEngineerDeviceDao.getNaturalFloorByLogicalFloor(deviceUnique, floor);
                if (StringUtils.isBlank(naturalFloor)) {
                    return -2;
                }
                params.put("naturalFloor", naturalFloor);
            }
            return iAppEngineerDeviceDao.bindingDeviceQrCode(params);
        } else {
            return -1;
        }
    }

    /**
     * <desc>
     *      获取电梯设备的楼层
     * </desc>
     *
     * @return 电梯设备的楼层列表
     * @author wangzhi
     * @createDate 2020/03/11
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<String> getDeviceFloors(String projectId, String deviceUnique) throws Exception {
        return iAppEngineerDeviceDao.getDeviceFloors(projectId, deviceUnique);
    }
}
